﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

//Needed for the accordion pane
using AjaxControlToolkit;

using AmkeniCrossLoginApplication.Data_Access;
//Needed for the dataset
using System.Data;


namespace Amkeni_System.CO
{
    public partial class Progs_Projects : System.Web.UI.Page
    {
        protected void Page_PreInit(object sender, EventArgs e)
        {
            /*if (User.IsInRole("IP"))
            {
                MasterPageFile = "~/MasterPages/Project.Master";
            }
            if (User.IsInRole("Programme"))
            {
                MasterPageFile = "~/MasterPages/Programme.Master";
            }
            if (User.IsInRole("Administrator"))
            {
                MasterPageFile = "~/MasterPages/CO.Master";
            }
            if (User.IsInRole("IP") && User.IsInRole("Programme"))
            {
                MasterPageFile = "~/MasterPages/ProgProject.Master";
            }*/
        }

        Amkeni_DatabaseEntities db = new Amkeni_DatabaseEntities();

        protected void Page_Load(object sender, EventArgs e)
        {
            //BindAccordion();
        }

        protected void AddProjMetaDataButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Amkeni_System/CO/Edit/AddIPMetadata.aspx", true);

        }

        protected void AddProjButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Amkeni_System/CO/Edit/AddProject.aspx", true);
        }

        //public void BindAccordion()
        //{
        //    using (Amkeni_DatabaseEntities db = new Amkeni_DatabaseEntities())
        //    {
        //        try
        //        {
        //            var programs = (from progs in db.programs select progs);
        //            MyAccordion.DataSource = programs;
        //            MyAccordion.DataBind();
        //        }
        //        catch (Exception ex)
        //        {
        //            Error.Text = "Database Error " + ex.Message + " " + ex.StackTrace;
        //            Error.Visible = true;
        //        }
        //    }
        //}

        protected void MyAccordion_ItemDataBound(object sender, AjaxControlToolkit.AccordionItemEventArgs e)
        {
            if (e.ItemType == AccordionItemType.Content)
            {
                //EntityDataSource ed = (EntityDataSource)e.AccordionItem.FindControl("EntityDataSource1");

                Label proj_label = (Label)e.AccordionItem.FindControl("proj_label");
                GridView grid1 = (GridView)e.AccordionItem.FindControl("ProjectGridView");
                GridView grid2 = (GridView)e.AccordionItem.FindControl("GridView1");
                BindGrid(grid1, proj_label.Text.Trim());
                BindGrid(grid2, proj_label.Text.Trim());
                //ed.WhereParameters["program_id"].DefaultValue = proj_label.Text;

                //try
                //{
                //    grid.DataSource = ed;
                //    grid.DataBind();
                //}
                //catch (Exception ex)
                //{
                //    Error.Text = "DataBinding Error " + ex.Message + " " + ex.StackTrace;
                //    Error.Visible = true;
                //}




                //GridView grid = (GridView)e.AccordionItem.FindControl("ProjectGridView");
                //Label proj_label = (Label)e.AccordionItem.FindControl("proj_label");
                //BindGrid(grid, proj_label.Text);
            }
        }

        private void BindGrid(GridView grid, string progID)
        {
            try
            {
                int Program_ID = int.Parse(progID.Trim());

                var myProjects = (from p in db.implementing_partner where p.outcome_id == Program_ID select p);
                grid.DataSource = myProjects.ToList();
                grid.DataBind();
            }
            catch (Exception ex)
            {
                Error.Text = "Database Error " + ex.Message + " " + ex.StackTrace;
                Error.Visible = true;
            }
        }

        protected void ProjectGridView_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView myGrid = (GridView)sender;
            myGrid.EditIndex = e.NewEditIndex;

            //.Values[1] represents the outcome_id datakeyname
            //Values[ip_id, outcome_id]
            BindGrid(myGrid, myGrid.DataKeys[0].Values[1].ToString());
        }

        protected void ProjectGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView myGrid = (GridView)sender;
            myGrid.EditIndex = -1;
            BindGrid(myGrid, myGrid.DataKeys[0].Values[1].ToString());
        }

        protected void ProjectGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridView myGrid = (GridView)sender;
            try
            {
                TextBox ProjectTX = (TextBox)myGrid.Rows[e.RowIndex].FindControl("TxProject");
                TextBox AbbrevTX = (TextBox)myGrid.Rows[e.RowIndex].FindControl("TxAbbreviation");
                int ProjID = int.Parse(myGrid.DataKeys[e.RowIndex].Values[0].ToString());

                implementing_partner updateProject = db.implementing_partner.First(p => p.ip_id == ProjID);
                updateProject.name = ProjectTX.Text.Trim();
                updateProject.abbreviation = AbbrevTX.Text.Trim();
                db.SaveChanges();

                myGrid.EditIndex = -1;
                BindGrid(myGrid, myGrid.DataKeys[e.RowIndex].Values[1].ToString());
            }
            catch (Exception ex)
            {
                Error.Text = "Database Error " + ex.Message + " " + ex.StackTrace;
                Error.Visible = true;
            }
        }

        protected void ProjectGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            GridView myGrid = (GridView)sender;
            try
            {
                long ProjID = long.Parse(myGrid.DataKeys[e.RowIndex].Values[0].ToString());

                implementing_partner delProject = db.implementing_partner.First(p => p.ip_id == ProjID);

                db.implementing_partner.Remove(delProject);
                db.SaveChanges();

                BindGrid(myGrid, myGrid.DataKeys[e.RowIndex].Values[1].ToString());
            }
            catch (Exception ex)
            {
                Error.Text = "Database Error " + ex.Message + " " + ex.StackTrace;
                Error.Visible = true;
            }
        }

        

        //protected void ProjectGridView_RowEditing(object sender, GridViewEditEventArgs e)
        //{
        //    GridView grid = (GridView)MyAccordion.FindControl("ProjectGridView");
            
        //    grid.EditIndex = e.NewEditIndex;
        //    grid.DataBind();
        //}

        //private void BindGrid(GridView grid, string program_txt)
        //{
        //    using (Amkeni_DatabaseEntities db = new Amkeni_DatabaseEntities())
        //    {
        //        try
        //        {
        //            //this was the root cause of getting error of 2 references to two datasources
        //            int txt = int.Parse(program_txt);

        //            var my_project = (from projs in db.projects
        //                              where projs.program_id == txt
        //                              select projs);
        //            if (my_project != null)
        //            {
        //                grid.DataSource = my_project;
        //                grid.DataBind();
        //            }
        //            else if (my_project == null)
        //            {
        //                Error.Text = "Nothing was retrieved from the database!!!!";
        //                Error.Visible = true;
        //            }
        //        }
        //        catch (Exception ex)
        //        {
        //            Error.Text = "Database Error " + ex.Message + " " + ex.StackTrace;
        //            Error.Visible = true;
        //        }
        //    }
        //}
    }
}